﻿using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using YawetagDemo.Framework.Data;

namespace YawetagDemo.Framework.Server.Data
{
    [DataContract(IsReference = true)]
    public class WCFBand : IBand
    {
        private WCFBand(IBand band, IDictionary<object, object> references)
        {
            references.Add(band, this);
            
            BandID = band.BandID;

            Name = band.Name;

            Artists = band.Artists.Select(i => WCFArtist.Get(i, references)).ToList();
            Albums = band.Albums.Select(i => WCFAlbum.Get(i, references)).ToList();
        }

        public static WCFBand Get(IBand band, IDictionary<object, object> references)
        {
            return (WCFBand)(references.ContainsKey(band) ? references[band] : new WCFBand(band, references));
        }

        [DataMember] public int BandID { get; private set; }

        [DataMember] public string Name { get; private set; }

        [DataMember] public IList<WCFArtist> Artists { get; private set; }
        IList<IArtist> IBand.Artists { get { return Artists.OfType<IArtist>().ToList(); } }

        [DataMember] public IList<WCFAlbum> Albums { get; private set; }
        IList<IAlbum> IBand.Albums { get { return Albums.OfType<IAlbum>().ToList(); } }
    }
}
